/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.giohang;

import controller.login.CheckTaiKhoan;
import dao.ChiTietDonDatHangDAO;
import dao.DienThoaiDAO;
import dao.DonDatHangDAO;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import net.webservicex.Currency;
import net.webservicex.CurrencyConvertor;
import pojo.Chitietdondathang;
import pojo.Dienthoai;
import pojo.Dondathang;
import template.choosePage;
/**
 *
 * @author vinh
 */
@ManagedBean (name="GioHang")
@SessionScoped
public class GioHang {

    /**
     * Creates a new instance of GioHang
     */
    static private ArrayList<MotHangTrongGioHang> GioHang = new ArrayList();
    int vt=-1;
    private int sl=0;
    public GioHang() {
    }

    public void setGioHang(ArrayList<MotHangTrongGioHang> Gio) {
        GioHang = Gio;
    }
  
    public int getvt()
    {
        return vt;
    }
    public void setvt(int tt)
    {
        vt=tt;
        setSl(GioHang.get(tt).getSoLuong());
    }
    public void setAddHangTrongGio(int madt) {
        DienThoaiDAO dtdao=new DienThoaiDAO();
        Dienthoai dt=dtdao.get(madt);
        System.out.println(dt.getTendienthoai()+"###^");
        MotHangTrongGioHang hang = new MotHangTrongGioHang();
        hang.setGia(dt.getGiaban());
        hang.setSoLuong(1);
        hang.setTenDT(dt.getTendienthoai());
        hang.sethinh(dt.getHinhanh());
        hang.setmadt(dt.getMadienthoai());
        boolean add = GioHang.add(hang);
    }
public void attrListener(ActionEvent event){
 Long aa;
		aa = (Long) event.getComponent().getAttributes().get("vitri");
 vt=aa.intValue();
	}

    public void setDeleteHangTrongGio() {
       System.out.print(vt+"^^^");
        if (vt < GioHang.size()&&vt!=-1) {
            GioHang.remove(vt);
       
        }
        vt=-1;
    }

    public void setDeleteGioHang() {
        GioHang = new ArrayList();
    }

    public ArrayList<MotHangTrongGioHang> getGioHang() {

        return GioHang;
    }
    public void setUpdateSoLuong()
    {
          if(GioHang!=null)
           {
            if(vt<GioHang.size())
            {
            MotHangTrongGioHang record=new MotHangTrongGioHang();
            record=GioHang.get(vt);
            record.setSoLuong(getSl());
            GioHang.set(vt, record);
            }
           }
    }
      public double conver()
    {
        Currency from=Currency.USD;
        Currency to =Currency.VND;
        CurrencyConvertor  cc =new CurrencyConvertor();      
       return  cc.getCurrencyConvertorSoap().conversionRate(from, to);
    }
    public void setThanhToan(String httt)
    {
         if (GioHang != null) {
            DonDatHangDAO ddhdao = new DonDatHangDAO();
            List<Dondathang> list = ddhdao.getList();
            Dondathang ddh = new Dondathang();
            ddh.setTaikhoan(CheckTaiKhoan.taikhoan);
            int ddhmax = 0;
            for (int i = 0; i < list.size(); i++) {
                int matk = Integer.parseInt(list.get(i).getMadondathang().replaceFirst("dh", ""));
                if (ddhmax < matk) {
                    ddhmax = matk;
                }
            }
            ddhmax++;
            ddh.setMadondathang("dh" + ddhmax);
            ddh.setPhuongthucthanhtoan(httt);
            ddhdao.saveorupdate(ddh);
            int tongtien = 0;
            for (int i = 0; i < GioHang.size(); i++) {
                ChiTietDonDatHangDAO ctdao = new ChiTietDonDatHangDAO();
                List<Chitietdondathang> listct = ctdao.getList();
                DienThoaiDAO dtdao = new DienThoaiDAO();
                Dienthoai dt = dtdao.get(GioHang.get(i).getmadt());
                //////////////////
                dt.setSoluongban(dt.getSoluongban()+GioHang.get(i).getSoLuong());
                dtdao.saveorupdate(dt);
                //////////////////
                Chitietdondathang ct = new Chitietdondathang();
                ct.setDondathang(ddh);
                ct.setDienthoai(dt);
                ct.setGia(GioHang.get(i).getGia());
                ct.setSoluong(GioHang.get(i).getSoLuong());
                int ctmax = 0;
                for (int ii = 0; ii < listct.size(); ii++) {
                    int matk = listct.get(ii).getStt();
                    if (ctmax < matk) {
                        ctmax = matk;
                    }
                }
                ctmax++;
                ct.setStt(ctmax);
                tongtien += ct.getSoluong() * ct.getGia();
                ctdao.saveorupdate(ct);
            }

            ///////////////////////////////////////////////
            Calendar cal = Calendar.getInstance();

            /////////////////////////////////////////////
            ddh.setTongtien(tongtien);
            ddh.setNgaydathang(cal.getTime());
            ddhdao.saveorupdate(ddh);
            this.setDeleteGioHang();
        }
         choosePage.page="TrangChu";

    }
public int getTongtienUSD()
{       int tongtien=0;
    if(GioHang!=null)
    {
       
          for (int i = 0; i < GioHang.size(); i++) {
                  tongtien += GioHang.get(i).getSoLuong()* GioHang.get(i).getGia();
          }
    }
    System.out.print("conver():" + conver());
    System.out.print("tongtien:" + tongtien);
    return (int) (tongtien/conver());
}
    /**
     * @return the sl
     */
    public int getSl() {
        return sl;
    }

    /**
     * @param sl the sl to set
     */
    public void setSl(int sl) {
        this.sl = sl;
    }
}
